package com.gzw.shop.activity;

import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.gzw.shop.App;
import com.gzw.shop.Contans;
import com.gzw.shop.R;
import com.gzw.shop.adapter.BaseAdapter;
import com.gzw.shop.adapter.CardViewtemDecortion;
import com.gzw.shop.adapter.MyOrderAdapter;
import com.gzw.shop.bean.Order;
import com.gzw.shop.http.OkHttpHelper;
import com.gzw.shop.http.SpotsCallBack;
import com.gzw.shop.widget.GToolbar;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import okhttp3.Response;

/**
 * Created by JACKYGUO on 2017-05-16.
 */

public class MyOrderActivity extends BaseActivity implements TabLayout.OnTabSelectedListener {

    public static final int STATUS_ALL = 1000;
    public static final int STATUS_SUCCESS = 1;//支付成功的订单
    public static final int STATUS_PAY_FAIL = -2;//支付失败的订单
    public static final int STATUS_PAY_WAIT = 0;//待支付订单
    private int status = STATUS_ALL;

    @BindView(R.id.toolbar)
    GToolbar toolbar;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private OkHttpHelper helper = OkHttpHelper.getInstance();

    private MyOrderAdapter adapter;


    @Override
    public int getLayoutId() {
        return R.layout.activity_my_order;
    }

    @Override
    public void initView() {

        initTabLayout();
        toolbar.setIvLeftOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void initTabLayout() {

        TabLayout.Tab tab = tabLayout.newTab();
        tab.setText("全部订单");
        tab.setTag(STATUS_ALL);
        TabLayout.Tab tab1 = tabLayout.newTab();
        tab1.setText("支付成功");
        tab1.setTag(STATUS_SUCCESS);
        TabLayout.Tab tab2 = tabLayout.newTab();
        tab2.setText("待支付");
        tab2.setTag(STATUS_PAY_WAIT);
        TabLayout.Tab tab3 = tabLayout.newTab();
        tab3.setText("支付失败");
        tab3.setTag(STATUS_PAY_FAIL);

        tabLayout.addTab(tab);
        tabLayout.addTab(tab1);
        tabLayout.addTab(tab2);
        tabLayout.addTab(tab3);

        tabLayout.addOnTabSelectedListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {

        status = (int) tab.getTag();
        getOrders();

    }

    /**
     * 获取订单列表
     */
    private void getOrders() {

        Long user_id = App.getInstance().getUser().getId();

        Map<String, Object> params = new HashMap<>();
        params.put("user_id", user_id);
        params.put("status", status);

        helper.post(Contans.API.ORDER_LIST, params, new SpotsCallBack<List<Order>>(this) {
            @Override
            public void onSuccess(Response response, List<Order> datas) {

                showOrders(datas);

            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }
        });


    }

    private void showOrders(List<Order> datas) {

        if (adapter == null) {
            adapter = new MyOrderAdapter(this, datas);
            recyclerView.setAdapter(adapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.addItemDecoration(new CardViewtemDecortion());

            adapter.setListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void OnClick(View view, int position) {
                    toDetailActivity(position);
                }
            });
        } else {
            adapter.refreshData(datas);
            recyclerView.setAdapter(adapter);
        }
    }

    /**
     * 进入详情页面
     *
     * @param position
     */
    private void toDetailActivity(int position) {


    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

}
